package com.lsw.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.lsw.R;
import com.lsw.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 通用的dialog样式
 *
 * @author lsw
 * @Date 2016-11-18
 */
public class PubDialogUtil {
    public Dialog alertDialog;
    // protected Dialog dialog;
    protected Context context;
    // 对话框标题
    protected TextView pub_tip;

    public PubDialogUtil(Context context, OnClickListener cancelClick) {
        this.context = context;
        init(cancelClick);
    }

    public PubDialogUtil(Context context) {
        this.context = context;
        init(null);
    }

    @SuppressLint("InflateParams")
    @SuppressWarnings("deprecation")
    private void init(OnClickListener cancelClickCustom) {
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.pub_dialog_alert, null);
        view.setMinimumWidth(display.getWidth());
        layoutBtns = view.findViewById(R.id.layoutBtns);
        pub_tip = (TextView) view.findViewById(R.id.pub_tip);
        btnOkBottom = (Button) view.findViewById(R.id.btnOkBottom);
        btnOk = (Button) view.findViewById(R.id.btnOk);
        view.findViewById(R.id.btnCancel).setOnClickListener(
                cancelClickCustom == null ? onCancelClick : cancelClickCustom);
        btnCacle = (Button) view.findViewById(R.id.btnCancel);

        alertDialog = new Dialog(context, R.style.dialogStyle);
        alertDialog.setContentView(view);
        Window dialogWindow = alertDialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);
    }

    private View childView;

    /**
     * 显示通用的对话框
     *
     * @param child
     * @param onOkClick
     * @param showBtnBottom
     */
    public void showDialog(View child, OnClickListener onOkClick,
                           boolean showBtnBottom, String strBtn) {
        ViewGroup viewGroup = (ViewGroup) view
                .findViewById(R.id.frameLayout);
        viewGroup.removeAllViews();
        viewGroup.addView(child);
        if (showBtnBottom) {
            btnOkBottom.setVisibility(View.VISIBLE);
            layoutBtns.setVisibility(View.GONE);
            btnOkBottom.setText(strBtn);
        }
        if (onOkClick != null) {
            btnOk.setOnClickListener(onOkClick);
        } else {
            btnOk.setOnClickListener(onCancelClick);
        }
        // btnOkBottom.setOnClickListener(onCancelClick);
        if (!alertDialog.isShowing()) {
            alertDialog.show();
        }
    }

    /**
     * 显示通用的对话框带标题
     *
     * @param child
     * @param onOkClick
     * @param showBtnBottom
     */
    public void showDialogtitle(String title, View child,
                                OnClickListener onOkClick, boolean showBtnBottom, String strBtn) {
        pub_tip.setText(title);
        if (childView == null) {
            childView = child;
            ViewGroup viewGroup = (ViewGroup) view
                    .findViewById(R.id.frameLayout);
            viewGroup.addView(child);
        }
        if (showBtnBottom) {
            btnOkBottom.setVisibility(View.VISIBLE);
            layoutBtns.setVisibility(View.GONE);
            btnOkBottom.setText(strBtn);
            btnOkBottom.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    dismiss();
                }
            });
        }
        if (onOkClick != null) {
            btnOk.setOnClickListener(onOkClick);
        } else {
            btnOk.setOnClickListener(onCancelClick);
        }
        // btnOkBottom.setOnClickListener(onCancelClick);
        if (!alertDialog.isShowing()) {
            alertDialog.show();
        }
    }

    public void showDialogOnlyText(String text, OnClickListener onOkClick,
                                   boolean showBtnBottom, String strBtn) {
//        if (childView == null) {
        childView = new TextView(context);
        ViewGroup viewGroup = (ViewGroup) view
                .findViewById(R.id.frameLayout);
        TextView tv = (TextView) childView;
        tv.setText(text);
        tv.setGravity(Gravity.CENTER_HORIZONTAL);
        tv.setTextColor(Color.BLACK);
        tv.setTextSize(14.0f);
        tv.setPadding(DensityUtil.dip2px(context, 20), 0,
                DensityUtil.dip2px(context, 20),
                DensityUtil.dip2px(context, 10));
        tv.setBackgroundColor(Color.WHITE);
        viewGroup.addView(childView);
//        }
        if (showBtnBottom) {
            btnOkBottom.setVisibility(View.VISIBLE);
            layoutBtns.setVisibility(View.GONE);
            btnOkBottom.setText(strBtn);
            btnOkBottom.setOnClickListener(onOkClick);
        } else {
            btnOkBottom.setVisibility(View.GONE);
            layoutBtns.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(strBtn)) {
                btnOk.setText(strBtn);
            }
            if (onOkClick != null) {
                btnOk.setOnClickListener(onOkClick);
            } else {
                btnOk.setOnClickListener(onCancelClick);
            }
        }
        if (!alertDialog.isShowing()) {
            alertDialog.show();
        }
    }

    /**
     * @param text          提示内容
     * @param onOkClick     确定监听
     * @param showBtnBottom 是否显示取消确定
     * @param strBtn        自定义确定按钮
     * @param isCan         点击其他地方是否消失
     */
    public void showDialogOnlyText(String text, OnClickListener onOkClick,
                                   boolean showBtnBottom, String strBtn, boolean isCan) {
//        if (childView == null) {

        childView = new TextView(context);
        ViewGroup viewGroup = (ViewGroup) view
                .findViewById(R.id.frameLayout);
        TextView tv = (TextView) childView;
        tv.setText(text);
        tv.setGravity(Gravity.CENTER_HORIZONTAL);
        tv.setTextColor(Color.BLACK);
        tv.setTextSize(14.0f);
        tv.setPadding(DensityUtil.dip2px(context, 20), 0,
                DensityUtil.dip2px(context, 20),
                DensityUtil.dip2px(context, 10));
        tv.setBackgroundColor(Color.WHITE);
        viewGroup.addView(childView);
//        }
        if (showBtnBottom) {
            btnOkBottom.setVisibility(View.VISIBLE);
            layoutBtns.setVisibility(View.GONE);
            btnOkBottom.setText(strBtn);
            btnOkBottom.setOnClickListener(onOkClick);
        } else {
            if (!TextUtils.isEmpty(strBtn)) {
                btnOk.setText(strBtn);
            }
            if (onOkClick != null) {
                btnOk.setOnClickListener(onOkClick);
            } else {
                btnOk.setOnClickListener(onCancelClick);
            }
        }
        alertDialog.setCancelable(isCan);
        if (!alertDialog.isShowing()) {
            alertDialog.show();
        }
    }

    /**
     * 不显示标题
     *
     * @param text          提示内容
     * @param onOkClick     确定监听
     * @param showBtnBottom 是否显示取消确定
     * @param strBtn        自定义确定按钮
     * @param isCan         点击其他地方是否消失
     */
    public void showDialognotitleText(String text, OnClickListener onOkClick,
                                      boolean showBtnBottom, String strBtn, boolean isCan) {
        pub_tip.setText(text);
        if (!showBtnBottom) {
            btnOkBottom.setVisibility(View.VISIBLE);
            layoutBtns.setVisibility(View.GONE);
            btnOkBottom.setText(strBtn);
            btnOkBottom.setOnClickListener(onOkClick);
        } else {
            if (!TextUtils.isEmpty(strBtn)) {
                btnOk.setText(strBtn);
            }
            if (onOkClick != null) {
                btnOk.setOnClickListener(onOkClick);
            } else {
                btnOk.setOnClickListener(onCancelClick);
            }
        }
        alertDialog.setCancelable(isCan);
        if (!alertDialog.isShowing()) {
            alertDialog.show();
        }
    }

    /**
     * 不显示标题
     *
     * @param text      提示内容
     * @param onOkClick 确定监听
     * @param cantext   自定义取消按钮
     * @param strtrue   自定义确定按钮
     * @param isCan     点击其他地方是否消失
     */
    public void showDialognotitlecanText(String text, OnClickListener onOkClick,
                                         String cantext, String strtrue, boolean isCan) {
        pub_tip.setText(text);

        if (!TextUtils.isEmpty(strtrue)) {
            btnOk.setText(strtrue);
        }
        if (!TextUtils.isEmpty(cantext)) {
            btnCacle.setText(cantext);
            btnCacle.setTextColor(context.getResources().getColor(R.color.black));
        }
        if (onOkClick != null) {
            btnOk.setOnClickListener(onOkClick);
        } else {
            btnOk.setOnClickListener(onCancelClick);
        }
        alertDialog.setCancelable(isCan);
        if (!alertDialog.isShowing()) {
            alertDialog.show();
        }
    }


    // 带标题的对话框
    public void showDialogtitleText(String title, String text,
                                    OnClickListener onOkClick, boolean showBtnBottom, String strBtn) {
        pub_tip.setText(title);
//        if (childView == null) {
        childView = new TextView(context);
        ViewGroup viewGroup = (ViewGroup) view
                .findViewById(R.id.frameLayout);
        TextView tv = (TextView) childView;
        tv.setText(text);
        tv.setGravity(Gravity.CENTER_HORIZONTAL);
        tv.setTextColor(context.getResources().getColor(R.color.color_666666));
        tv.setTextSize(14.0f);
        tv.setPadding(DensityUtil.dip2px(context, 20), 0,
                DensityUtil.dip2px(context, 20),
                DensityUtil.dip2px(context, 10));
        tv.setBackgroundColor(Color.WHITE);
        viewGroup.addView(childView);
//        }
        if (showBtnBottom) {
            btnOkBottom.setVisibility(View.VISIBLE);
            layoutBtns.setVisibility(View.GONE);
            btnOkBottom.setText(strBtn);
            btnOkBottom.setOnClickListener(onOkClick);
        } else {
            if (!TextUtils.isEmpty(strBtn)) {
                btnOk.setText(strBtn);
            }
            if (onOkClick != null) {
                btnOk.setOnClickListener(onOkClick);
            } else {
                btnOk.setOnClickListener(onCancelClick);
            }
        }
        if (!alertDialog.isShowing()) {
            alertDialog.show();
        }
    }

    // 带标题的列表对话框
    public void showDialogtitleList(String title, String text,
                                    final DialogInterface dialogInterface, boolean showBtnBottom, String strBtn) {
        pub_tip.setText(title);
//        if (childView == null) {
        ViewGroup viewGroup = (ViewGroup) view
                .findViewById(R.id.frameLayout);
        viewGroup.removeAllViews();
        childView = LayoutInflater.from(context).inflate(R.layout.dialog_listview, null);
        ListView listView = childView.findViewById(R.id.listview);
        final DialogListViewAdapter dialogListViewAdapter = new DialogListViewAdapter(context);
        listView.setAdapter(dialogListViewAdapter);
        List<String> list = new ArrayList<>();
        list.add("现金购买");
        list.add("未购买");
        dialogListViewAdapter.setData(list);
        viewGroup.addView(childView);
        if (showBtnBottom) {
            btnOkBottom.setVisibility(View.VISIBLE);
            layoutBtns.setVisibility(View.GONE);
            btnOkBottom.setText(strBtn);
            btnOkBottom.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (dialogInterface != null)
                        dialogInterface.setOk(dialogListViewAdapter.getList().get(dialogListViewAdapter.getSeclect()));
                }
            });
        } else {
            if (!TextUtils.isEmpty(strBtn)) {
                btnOk.setText(strBtn);
            }
            if (dialogInterface != null) {
                btnOk.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialogInterface.setOk(dialogListViewAdapter.getList().get(dialogListViewAdapter.getSeclect()));
                    }
                });
            } else {
                btnOk.setOnClickListener(onCancelClick);
            }
        }
        if (!alertDialog.isShowing()) {
            alertDialog.show();
        }
    }

    public void dismiss() {
        ViewGroup viewGroup = (ViewGroup) view
                .findViewById(R.id.frameLayout);
        childView = null;
        alertDialog.dismiss();

    }


    private View view;
    private View layoutBtns;
    public Button btnOkBottom;
    private OnClickListener onCancelClick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            alertDialog.dismiss();
        }
    };
    /**
     * 确定按钮
     */
    public Button btnOk;
    /**
     * 取消按钮
     */
    public Button btnCacle;


}
